package cn.oslanka.mls.utils;

import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Toast;

import com.viewpagerindicator.TabPageIndicator;

import java.util.List;

/**
 * Created by T400 on 15-6-20.
 */
public class IndicatorInit {
    private String title[];
    private TabPageIndicator indicator;
    private ViewPager pager;
    private List<Fragment> fragments;
    private Context context;
    private FragmentManager fragmentManager;
    public IndicatorInit(Context context,FragmentManager fragmentManager,String[] title, TabPageIndicator indicator, ViewPager pager, List<Fragment> fragments) {
        this.context=context;
        this.fragmentManager=fragmentManager;
        this.title = title;
        this.indicator = indicator;
        this.pager = pager;
        this.fragments = fragments;
        init();
    }



    private void init() {
        pager.setAdapter(new FragmentPagerAdapter(fragmentManager) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return title[position];
            }
        });
        indicator.setViewPager(pager);//关联必须先设置adapter ，不然报错的
        indicator.setVisibility(View.VISIBLE);
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                Toast.makeText(context, title[arg0], Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });




    }


}
